// src/axios.js
import axios from 'axios';

const instance = axios.create({
  baseURL: 'http://localhost:3000', // 设置基础URL
  timeout: 10000, // 设置请求超时
  headers:{
    "Content-Type":"application/json;charset=urf-8",//表单数据转化
}
});

// 请求拦截器
instance.interceptors.request.use(
  config => {
 
    const token = localStorage.getItem('token'); // 从 localStorage 获取 token
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`; // 添加 Authorization 头
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  response => {
    return response.data; // 处理响应数据
  },
  error => {
    return Promise.reject(error);
  }
);

export default instance;
